<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item v-for="(item, index) in breadList" :key="index" :to="{ path: item.path }">{{ item.meta.title
        }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div>
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" status-icon>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username" />
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <mavon-editor v-model="ruleForm.content" style="width: 100%" ref="md" />
          </el-form-item>
          <el-form-item label="点赞数" prop="like">
            <el-input type="number" max="5" min="1" v-model="ruleForm.like" />
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button type="primary" @click="submitForm(ruleFormRef)">新增</el-button>
        <el-button @click="resetForm(ruleFormRef)">重置表单</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { useRouter, useRoute } from "vue-router";
import { ref, reactive, watch } from "vue";
import request from "../../utils/request";
import { ElNotification } from "element-plus";
import "mavon-editor/dist/css/index.css";
export default {
  setup() {
    let breadList = []; // 路由集合
    const route = useRoute();
    const categoryData = ref([]);
    // console.log(categoryData.value);
    const getBreadcrumb = (url) => {
      let matched = url.matched;
      //如果不是首页
      if ("Home" == matched[0].name) {
        matched[0].meta = { title: "系统首页" };
      }
      breadList = matched;
    };
    watch(
      route,
      (newValue, oldValue) => {
        getBreadcrumb(newValue);
      },
      { deep: true, immediate: true }
    );

    const resetForm = (formEl) => {
      if (!formEl) return;
      formEl.resetFields();
    };
    // 富文本编辑器开始
    let md = ref(null);
    const submitForm = async (formEl) => {
      if (!formEl) return;
      await formEl.validate((valid, fields) => {
        if (valid) {
          request
            .request({
              url: "/comment/add",
              method: "post",
              data: {
                username: ruleForm.username,
                content: ruleForm.content,
                like: ruleForm.like,
              },
            })
            .then((res) => {
              if (res.data.code == 200) {
                formEl.resetFields();
                ElNotification({
                  title: "添加成功",
                  message: "成功添加用户评论",
                });
              } else {
                ElNotification({
                  title: "添加失败",
                  message: "请联系管理员检查失败原因",
                });
              }
            })
            .catch((e) => {
              ElNotification({
                title: "添加失败",
                message: "请联系管理员检查失败原因",
              });
            });
        } else {
          ElNotification({
            title: "校验失败",
            message: "请重新填写信息",
          });
        }
      });
    };

    // const tableData = ref([]);
    const ruleForm = reactive({
      username: "",
      content: "",
      like: "0",
    });
    const rules = reactive({
      username: [
        {
          required: false,
          message: "请输入用户姓名",
          trigger: "blur",
        },
      ],
      content: [
        {
          required: false,
          message: "请输入评论内容",
          trigger: "blur",
        },
      ],
      like: [
        {
          required: true,
          message: "请选择点赞数",
          trigger: "change",
        },
      ],
    });

    //获取分类数据
    const getCategory = () => {
      request
        .request({
          url: "/category/getAllCategory",
          method: "get",
        })
        .then((res) => {
          //获取数据
          res.data.data.forEach((e) => {
            categoryData.value.push(e);
          });
        })
        .catch((e) => {
          console.log(e);
        });
    };
    getCategory();
    const ruleFormRef = ref();
    const centerDialogVisible = ref(false);
    return {
      breadList,
      resetForm,
      ruleFormRef,
      submitForm,
      ruleForm,
      rules,
      centerDialogVisible,
      categoryData,
      getCategory,
    };
  },
};
</script>
<style scoped></style>
